package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Dormitory;
import org.example.entity.Student;
import org.example.form.SearchForm;
import org.example.form.StudentForm;
import org.example.mapper.DormitoryMapper;
import org.example.mapper.StudentMapper;
import org.example.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.util.CommonUtil;
import org.example.vo.PageVO;
import org.example.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    public Boolean saveStudent(Student student) {
        //1.添加学生信息
        //学生默认入住
        student.setState("入住");
        student.setCreateDate(CommonUtil.createDate());
        int insert = studentMapper.insert(student);
        if (insert <= 0) return false;

        //3.修改宿舍状态
        Dormitory dormitory = dormitoryMapper.selectById(student.getDormitoryId());
        if (dormitory.getAvailable() == 0) return false;
        dormitory.setAvailable(dormitory.getAvailable() - 1);
        int i = dormitoryMapper.updateById(dormitory);
        if (i <= 0) return false;
        return true;

    }

    @Override
    public PageVO list(Integer page, Integer size) {
        Page<Student> studentPage = studentMapper.selectPage(new Page<>(page, size), null);

        List<Student> studentlist = studentPage.getRecords();
        List<StudentVO> students =  new ArrayList<StudentVO>();
        for (Student student : studentlist) {
           StudentVO studentVO = new StudentVO();
           BeanUtils.copyProperties(student, studentVO);//将学生信息复制到学生VO中
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            students.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(students);
        pageVO.setTotal(studentPage.getTotal());
        return  pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Student> studentPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Student> studentPage1 = null;

        List<StudentVO> students =  new ArrayList<StudentVO>();
        if(searchForm.getValue().equals("")){
            studentPage1 = studentMapper.selectPage(studentPage, null);
            studentPage1.getRecords();

        }else{
            QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Student> like = queryWrapper.like(searchForm.getKey(), searchForm.getValue());
           studentPage1 = this.studentMapper.selectPage(studentPage, like);
           studentPage1.getRecords();
        }
        for (Student student : studentPage1.getRecords()) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);//将学生信息复制到学生VO中
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            studentVO.setDormitoryName(dormitory.getName());
            students.add(studentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(students);
        pageVO.setTotal(studentPage1.getTotal());
        return  pageVO;
    }


    @Override
    public Boolean updateById(StudentForm studentForm) {
        //更新学生信息
        Student student = new Student();
        BeanUtils.copyProperties(studentForm, student);
        int update = this.studentMapper.updateById(student);
        if(update != 1) return false;
        //更新宿舍数据
        if(!studentForm.getDormitoryId().equals(studentForm.getOrdDormitoryId())){
            //old+1，new-1
            try {
                this.dormitoryMapper.addAvailable(studentForm.getOrdDormitoryId());
                this.dormitoryMapper.subAvailable(studentForm.getDormitoryId());
            } catch (Exception e) {
                return false;
            }
        }
        return true;
    }

    public   Boolean deleteById(Integer id){
        //更新宿舍数据
        Student student = this.studentMapper.selectById(id);
        try {
            Dormitory dormitory = this.dormitoryMapper.selectById(student.getDormitoryId());
            if (dormitory.getType()>dormitory.getAvailable()) {
                this.dormitoryMapper.addAvailable(student.getDormitoryId());
            }
        } catch (Exception e) {
            return false;
        }
        //删除学生信息
        int delete = this.studentMapper.deleteById(id);
        if(delete != 1) return false;

        return true;
    }

    @Override
    public List<Student> findStudentByDormitoryId(int dormitoryId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dormitory_id", dormitoryId);
        queryWrapper.eq("state", "入住");
        List<Student> students = this.studentMapper.selectList(queryWrapper);
        return students;
    }
}
